/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.web.util;

import java.util.Arrays;
import java.util.List;

import org.jefb.util.JefbUtils;

/**
 * @author alexej
 * 
 */
public enum FileType {
	// images
	JPG(".jpg", "jpeg.png"), JPEG(".jpeg", "jpeg.png"), PNG(".png", "png.png"), GIF(
			".gif", "gif.png"),
	// text
	TXT(".txt", "txt.png"), LOG(".log", "log.png"), CONF(".conf", "txt.png"),
	// sound
	MP3(".mp3", "mp3.png"),
	// bin
	EXE(".exe", "exec_wine.png"),

	// archives
	ZIP(".zip", "zip.png"), TAR(".tar", "archive.png"), ARJ(".arj",
			"archive.png"), BZ2(".bz2", "archive.png"), CBZ(".cbz",
			"archive.png"), GZ(".gz", "archive.png"), IHA(".iha", "archive.png"), IHZ(
			".ihz", "archive.png"), PAR(".par", "archive.png"), TGZ(".tgz",
			"archive.png"), WAR(".war", "archive.png"), EAR(".ear", "archive.png"),

	// package
	DEB(".deb", "deb.png"), RPM(".rpm", "rpm.png"),

	// office
	DOC(".doc", "documents.png"), ODT(".odt", "documents.png"), ODS(".ods",
			"spreadsheet.png"), PDF(".pdf", "pdf.png"),

	// cd images
	ISO(".iso", "cdimage.png"), NRG(".nrg", "cdimage.png"),

	// shell scripts
	SH(".sh", "shellscript.png"), BAT(".bat", "shellscript.png"),

	// source/script code
	XSD(".xml", "xml.png", "xml"), XML(".xml", "xml.png", "xml"), HTM(".htm",
			"html.png", "html"), HTML(".html", "html.png", "html"), XHTML(
			".xhtml", "html.png", "html"), C(".c", "source_c.png"), CPP(".cpp",
			"source_cpp.png"), H(".h", "source_h.png"), JAVA(".java",
			"source_java.png", "java"), PHP(".php", "source_php.png", "php"), PY(
			".py", "source_py.png", "py"), PYC(".pyc", "source_pyc.png"), RB(
			".rb", "source_ruby.png", "ruby"), SQL(".sql", "source_sql.png",
			"sql"), JS(".js", "source_js.png", "js"), CSS(".css", "txt.png",
			"css"), GROOVY(".groovy", "txt.png", "groovy"),

	UNKNOWN("", "unknown.png");

	private String ext;
	private String fileName;
	private String codemirrorSyntax;

	private FileType(String ext, String fileName) {
		this.ext = ext;
		this.fileName = fileName;
	}

	private FileType(String ext, String fileName, String codemirrorSyntax) {
		this.ext = ext;
		this.fileName = fileName;
		this.codemirrorSyntax = codemirrorSyntax;
	}

	public static List<FileType> getAllowedImageFiles() {
		return Arrays.asList(new FileType[] { JPG, JPEG, PNG, GIF });
	};
	
	public static List<FileType> getAllowedZipFiles() {
		return Arrays.asList(new FileType[] { ZIP, WAR, EAR });
	};
	
	public static boolean isAllowedZipFile(String fileName) {
		String ext = JefbUtils.extractExtension(fileName);
		FileType fileType = FileType.getByExtension(ext);
		return getAllowedZipFiles().contains(fileType);
	}

	public static boolean isAllowedTextFile(String fileName) {
		String ext = JefbUtils.extractExtension(fileName);
		FileType fileType = FileType.getByExtension(ext);
		return getAllowedTextFiles().contains(fileType);
	}

	public static boolean isAllowedImageFile(String fileName) {
		String ext = JefbUtils.extractExtension(fileName);
		FileType fileType = FileType.getByExtension(ext);
		return getAllowedImageFiles().contains(fileType);
	}

	public static List<FileType> getAllowedTextFiles() {
		return Arrays.asList(new FileType[] { TXT, CONF, LOG, HTM, HTML, XHTML,
				C, CPP, H, JAVA, PHP, PY, PYC, RB, SH, BAT, XML, XSD, JS, SQL,
				CSS, GROOVY });
	}

	public static List<FileType> getCodemirrorAllowedTextFiles() {
		return Arrays.asList(new FileType[] { HTM, HTML, XHTML, JAVA, PHP, PY,
				RB, XML, XSD, JS, SQL, CSS, GROOVY });
	}

	public static FileType getByExtension(String ext) {
		for (FileType fileType : FileType.values()) {
			if (fileType.getExt().equals(ext)) {
				return fileType;
			}
		}
		return UNKNOWN;
	}

	public String getExt() {
		return ext;
	}

	public void setExt(String ext) {
		this.ext = ext;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setCodemirrorSyntax(String codemirrorSyntax) {
		this.codemirrorSyntax = codemirrorSyntax;
	}

	public String getCodemirrorSyntax() {
		return codemirrorSyntax;
	}

}